--[[
	Copyright (C) Udorn (Blackhand)
--]]

local viewType = "Checkbox"
local version = 1
local YakaGui = LibStub("YakaGui", true)
local info = YakaGui:GetViewInfo(viewType) 
if (info and info.version >= version) then 
	return 
end

yaka = yaka or {}
yaka.Checkbox = {}
yaka.Checkbox.prototype = {}
yaka.Checkbox.metatable = {__index = yaka.Checkbox.prototype}
setmetatable(yaka.Checkbox.prototype, {__index = yaka.View.prototype})

yaka.Checkbox.info = {viewType = viewType, version = version}
yaka.Checkbox.GetViewInfo = function()
	return yaka.Checkbox.info
end

local log = vendor.Debug:new("Checkbox")

local function _OnClick(this, button)
	local self = this.view
	if (self.cfg.setValue) then
		local info = {arg = self.cfg.arg, view = self, value = self.frame:GetChecked()}
		self.cfg.setValue(info)
	end
end

--[[ 
	Creates a new instance.
--]]
function yaka.Checkbox:new(cfgIn)
	local instance = setmetatable({}, self.metatable)
	instance:InitCfg(cfgIn)
	instance:InitFrame()
	return instance
end

function yaka.Checkbox.prototype:InitCfg(cfgIn)
	yaka.View.prototype.InitCfg(self, cfgIn)
	
	local cfg = self.cfg
	cfg.setValue = cfgIn.setValue
	cfg.getValue = cfgIn.getValue
end

function yaka.Checkbox.prototype:InitFrame()
	local frame = CreateFrame("Checkbutton", nil, UIParent, "UICheckButtonTemplate")
	frame.view = self
	
	frame:SetScript("OnClick", _OnClick)
	
	self.frame = frame
	yaka.View.prototype.InitFrame(self, cfgIn)
	
	if (self.cfg.getValue) then
		local checked = self.cfg.getValue({arg = self.cfg.arg, view = self})
		self.frame:SetChecked(checked)
	end
end

function yaka.Checkbox.prototype:Layout(x, y, width, height)
	log:Debug("Layout x [%s] y [%s] width [%s] height [%s]", x, y, width, height)
	yaka.View.prototype.Layout(self, x, y, width, height)
end

function yaka.Checkbox.prototype:SetChecked(checked)
	self.frame:SetChecked(checked)
end

-- register view

YakaGui:RegisterViewType(yaka.Checkbox)